#!/bin/bash
category="disk"

##硬盘扫的次数
##每次间隔10s
checkMax=3

##工作路径
work_path=$(dirname $(readlink -f $0))

smarfile=''

source ${work_path}/fun.devicehealth.sh

function getStrValue(){
	grepKey=$1
	grepFile=$2
	grep -i "${grepKey}" $grepFile |head -n 1| awk -F":" '{print $2}' | awk '{gsub(/^\s+|\s+$/, "");print}'

}

function getJsonValue(){
	key=$1
	json=$2

	cat $json |jq "$key" | sed 's/"//g'

}
function getDiskType(){
	name=$1
	rotational=`cat /sys/block/$1/queue/rotational`
	if [ $rotational -eq 0 ];then
		echo "SSD"
	else
		echo "HDD"
	fi
}


function getDeviceType(){
	lspci |grep -i 'SCSI controller' |grep 'Smart Array' > /dev/null
	if [ $? -eq 0 ];then
		smartDeviceType="-d cciss,$smartController"
	fi

}

function getDevicelabel(){
	diskLabel=$1
	if [[ "$smartDeviceType" != '' ]];then
		newLabel=`lsscsi -g|grep '/dev/'$diskLabel |awk '{print $7}'`
	fi

	if [[ "$newLabel" != '' ]];then
		echo $newLabel
	else
		echo "/dev/"$diskLabel
	fi

}

##通过磁盘标签/dev/sda sdb在lshw中获取需要的数据
function getDiskValueForLSHW(){
	diskLabel=$1
	grepKey=$2
	jsonLength=`cat /tmp/disk.lshw.json |jq '.| length'`
	for ((i=0; i<$jsonLength;i++))
	do
		cat /tmp/disk.lshw.json |jq ".[$i].logicalname" | grep $diskLabel >/dev/null
		if [ $? -eq 0 ];then
			cat /tmp/disk.lshw.json |jq ".[$i].$grepKey" | sed 's/"//g' | awk '{gsub(/^\s+|\s+$/, "");print}'

			break;
		fi
	done

}
smartDeviceType=''
smartController=0

getDeviceType

lshw -class disk -json >/tmp/disk.lshw.json

startCheck=0
while [[ $startCheck -eq 0 ]]; do

	lsblk -d |egrep -v 'NAME|loop|sr'> /dev/null
	if [ $? -eq 0 ];then
		break;
	fi


	if [ $checkMax -lt 1 ];then
		echo "无法识别到服务器上的硬盘"
		exit 1
	fi

	let checkMax--

	## sheep 10s
	sleep 10
done

for disk in `lsblk -d -o name |egrep -v 'NAME|loop|sr'`
do
	smarfile="/tmp/${disk}.smart.log"
	smartjson="/tmp/${disk}.smart.json"
	disklabel=$(getDevicelabel $disk)

	smartctl -a $smartDeviceType ${disklabel} > $smarfile


	cat $smarfile |grep 'USB bridge' > /dev/null
	if [  $? -eq 0 ];then
		continue
	fi

	smartctl -a $smartDeviceType ${disklabel} > $smarfile -j > $smartjson

	#sn=`getStrValue 'Serial Number' $smarfile`

	sn=`getJsonValue ".serial_number" $smartjson`

	##硬盘容量
	size=`getStrValue 'User Capacity' $smarfile`

    #size=`smartctl --info /dev/${disk} | grep "User Capacity" | awk -F':' '{print $2}'  |awk -F'[' '{print $2}'|sed 's/]//g'`
    if [[ "$size" == "" ]];then
        size=`lsblk -d  |grep ${disk} |awk '{print $4}'`
    else
    	size=`echo $size |awk -F'[' '{print $2}'|sed 's/]//g'`
    fi

	## 固件号
	firmwareversion=`getJsonValue ".firmware_version" $smartjson`

    ## 型号
    model=`getJsonValue ".model_name" $smartjson`
    if [ "$model" == "" ];then
    	model=`getStrValue 'Product:' $smarfile`
    fi

	## 厂家
	brand=`getJsonValue ".model_family" $smartjson`
    if [ " $brand" == '' ];then
    	   brand=`echo $model |awk '{print $1}'`
    fi
    #health=`smartctl -H /dev/${disk} |grep 'self-assessment' |awk -F':' '{print $2}' | awk '$1=$1'`
    health=`getDeviceHealth $smarfile`




    ## 获取硬盘的类型/ssd/hdd
    disk_type=`getDiskType $disk`

    ## 获取硬盘的转速度
	speed=`getJsonValue ".interface_speed.current.string" $smartjson`

	## 硬盘接口
	disk_interface=`getJsonValue ".device.protocol" $smartjson`
	if [[ "$disk_interface" == '' ]];then
		echo $disk_interface
		disk_interface='SATA'
	else
		disk_interface=`echo $disk_interface | awk '{print $1}'`
	fi

    echo "category=${category};brand=${brand};model=${model};sn=${sn};size=${size};speed=${speed};interface=${disk_interface};health=${health}%;lable=/dev/${disk};type=${disk_type};firmware_version=${firmwareversion}"
done

